<?php

namespace App\Admin\Extensions\ExcelExport;

use App\Models\Area;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class CarExpoter extends AbstractExporter
{
    public function export()
    {
        ini_set('memory_limit','500M');
        set_time_limit(0);//设置超时限制为0分钟
        Excel::create('旅游大巴', function($excel) {
            $excel->sheet('车辆', function($sheet) {
                // 这段逻辑是从表格数据中取出需要导出的字段
                $rows = collect($this->getData())->map(function ($item) {
                    $item['area_id'] = $item['area_id']?Area::find($item['area_id'])->title:'';
                    return array_only($item, ['id','area_id','scope', 'license_plate_number','driver','company']);
                });
                $sheet->prependRow(['ID', '区域','范围(0:省级1:市际2:县际3:县内)', '车牌号','随车司机','客运公司']);
                $sheet->row(1, function($row) {
                    $row->setAlignment('center');
                    $row->setFont(array(
                        'size'       => '12',
                        'bold'       =>  true
                    ));
                });
                $sheet->rows($rows);
            });
        })->export('xls');
    }
}